package com.tbit.api;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.manager.api.TerControlApi;
import com.tbit.manager.api.dto.TerControlDTO;
import com.tbit.uqbike.object.business.PageData;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.OrderLog;
import com.tbit.uqbike.object.pojo.ReturnBikeLog;
import com.tbit.uqbike.object.pojo.dto.CommonDTO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.AccountUserService;
import com.tbit.uqbike.service.business.ReturnBikeLogService;
import com.tbit.uqbike.service.business.TerControlService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.dao.core.RoleDao;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.uqbike.webmanager.util.page.PageDomain;
import com.tbit.uqbike.webmanager.util.page.TableSupport;
import com.tbit.uqbike.webmanager.util.sql.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
public class TerControlApiImpl implements TerControlApi {

    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private TerControlService terControlService;
    @Resource
    private ReturnBikeLogService returnBikeLogService;

    /**
     * @description: 获取车辆指令日志
     * @author: mo.shanyong
     * @date: 2025/2/26 16:38
     * @param: terControlDTOStr 搜索参数
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> queryOrderHis(String terControlDTOStr) {
        TerControlDTO terControlDTO = JSON.parseObject(terControlDTOStr, TerControlDTO.class);
        //获取当前登录用户转换为管理端的用户类
        LoginInfo loginInfo = this.getLoginInfo(terControlDTO.getAccountUserId(), terControlDTO.getBrandId(),
                terControlDTO.getAccountUserType(),terControlDTO.getAccountId());
        PageData<List<OrderLog>> listPageData = terControlService.queryOrderHis(loginInfo, terControlDTO.getRowCount(), terControlDTO.getPageNO(), terControlDTO.getStartDt(), terControlDTO.getEndDt(),
                terControlDTO.getUserCode(), null, null, null,null);
        List<OrderLog> data = listPageData.getData();
        Integer total = listPageData.getTotal();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("data",data);
        dataMap.put("total", Objects.isNull(total) ? 0 : total);
        return dataMap;
    }
    /**
     * @description: 获取还车指令日志
     * @author: mo.shanyong
     * @date: 2025/2/26 16:40
     * @param: terControlDTOStr
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> queryReturnBikeLog(String terControlDTOStr) {
        TerControlDTO terControlDTO = JSON.parseObject(terControlDTOStr, TerControlDTO.class);
        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setPageNO(terControlDTO.getPageNO());
        commonDTO.setRowCount(terControlDTO.getRowCount());
        commonDTO.setUserCode(terControlDTO.getUserCode());
        commonDTO.setStartDt(terControlDTO.getStartDt());
        commonDTO.setEndDt(terControlDTO.getEndDt());
        //分页
        this.startPage(terControlDTO.getPageNO(),terControlDTO.getRowCount());
        List<ReturnBikeLog> returnBikeLogs = returnBikeLogService.selectInfo(commonDTO);

        int total = (int) new PageInfo<>(returnBikeLogs).getTotal();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("data",returnBikeLogs);
        dataMap.put("total",total);
        return dataMap;
    }

    /**
     * @description: 获取登录用户
     * @author: mo.shanyong
     * @date: 2025/2/26 17:05
     * @param: userId 用户id
     * @param: brandId 品牌id
     * @param: accountUserType 用户类型
     * @param: accountId 区域id
     * @return: com.tbit.uqbike.object.value.LoginInfo
     */
    private LoginInfo getLoginInfo(Integer userId,Integer brandId,Integer accountUserType,Integer accountId){
        AccountUser accountUser = accountUserService.getAccountUserById(userId);
        return new LoginInfo(accountId,accountUserType, accountUser.getAccountUserId(),
                accountUser.getPhone(), accountUser.getName(), roleDao.selectRolePermissionByUserId(accountUser.getAccountUserId()), brandId);
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage(Integer pageNO,Integer rowCount) {
        PageHelper.startPage(pageNO, rowCount, "");
    }
}
